<template>
  <div>
    <a-modal
      :maskClosable="false"
      v-model="isNewShow"
      title="新建门店"
      :width="930"
      class="jlw_model_title"
      destroyOnClose>
      <new-change ref="newStore"></new-change>
      <template slot="footer">
        <a-button type="primary" class="primary_linear" @click="handleNew" :loading="addStoreLoading">创建门店</a-button>
      </template>
    </a-modal>
    <a-modal
      :maskClosable="false"
      v-model="isChangeShow"
      title="编辑门店"
      :width="930"
      class="jlw_model_title"
      destroyOnClose>
      <new-change ref="changeStore" :order="dataMsg"></new-change>
      <template slot="footer">
        <a-button type="primary" class="primary_linear" @click="handleChange" :loading="updateStoreLoading">保存</a-button>
      </template>
    </a-modal>
    <a-modal
      :maskClosable="false"
      v-model="isOwnershipShow"
      title="房源归属"
      :width="930"
      class="jlw_model_title    ownership"
      destroyOnClose>
      <housing-ownership ref="housingOwnership" :storesId="ownershipMsg" :mdmc="ownershipName"></housing-ownership>
      <template slot="footer">
        <a-button type="primary" class="primary_linear" @click="handleOwnership" :loading="ownershipLoading">保存</a-button>
      </template>
    </a-modal>
  </div>
</template>
<script>
import NewChange from './NewChange'
import HousingOwnership from './HousingOwnership'
export default {
  name: 'StoreModal',
  components: {
    NewChange, // 新增修改门店
    HousingOwnership // 房源归属
  },
  data () {
    return {
      isNewShow: false, // 新增门店
      isChangeShow: false, // 修改门店
      isOwnershipShow: false, // 房源归属
      dataMsg: {}, // 修改门店信息
      ownershipMsg: '', // 房源归属-门店id
      ownershipName: '', // 房源归属-mdmc
      ownershipLoading: false, // 房源归属 防止重复点击
      addStoreLoading: false, // 新建门店 防止重复点击
      updateStoreLoading: false // 修改门店 防止重复点击
    }
  },
  methods: {
    /**
     * 编辑弹窗 father调用
     * @param val 门店信息
    */
    changeStore (val) {
      this.dataMsg = Object.assign({}, val)
      this.isChangeShow = true
    },
    /** 新建弹窗 father调用 */
    addStore () {
      this.isNewShow = true
    },
    /** 房源归属弹窗father调用
    * @param val 门店id
    * @param mdmc 门店名称
    */
    housingOwnership (val, mdmc) {
      this.ownershipMsg = val
      this.ownershipName = mdmc
      this.isOwnershipShow = true
    },
    /** 新建门店 */
    async handleNew () {
      this.addStoreLoading = true
      try {
        const res = await this.$refs.newStore.handleSubmit('Add_STORE')
        if (res.code === 200) {
          this.$notification['success']({
            message: this.$t('message.successTitle'),
            description: this.$t('message.addStore')
          })
          this.addStoreLoading = false
          this.isNewShow = false
          // 新建接口
          this.$parent.searchAjax()
        }
      } catch {
        this.addStoreLoading = false
        console.log('验证不通过')
      }
    },
    /** 编辑门店 */
    async handleChange () {
      this.updateStoreLoading = true
      try {
        const res = await this.$refs.changeStore.handleSubmit('MODIFIER_STORE')
        if (res.code === 200) {
          this.$notification['success']({
            message: this.$t('message.successTitle'),
            description: this.$t('message.updateStore')
          })
          this.updateStoreLoading = false
          this.isChangeShow = false
          // 更新接口
          this.$parent.searchAjax()
        }
      } catch {
        this.updateStoreLoading = false
        console.log('验证不通过')
      }
    },
    // 房源归属
    async handleOwnership () {
      this.ownershipLoading = true
      try {
        const res = await this.$refs.housingOwnership.handleSubmit()
        if (res.code === 200) {
          this.$notification['success']({
            message: this.$t('message.successTitle'),
            description: this.$t('message.housingOwnership')
          })
          this.ownershipLoading = false
          this.isOwnershipShow = false
          // 更新接口
          this.$parent.searchAjax()
        }
      } catch {
        this.ownershipLoading = false
        console.log('验证不通过')
      }
    }
  }
}
</script>
